package com.wcgksoft.controller.equipment;

import com.wcgksoft.common.util.Layui;
import com.wcgksoft.common.util.comm.StringUtils;
import com.wcgksoft.entity.Equipment.Equipment;
import com.wcgksoft.service.Equipment.EquipmentService;
import net.sf.json.JSONObject;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 周权
 * @Date: 2020/03/06/14:58
 * @Description:设备管理
 */
@Controller
@RequestMapping(value = "system/equipment")
public class EquipmentController {
    @Autowired
    private EquipmentService equipmentService;
    /*
    * @Author: 周权
    * @Description:设备管理列表页面layui的table加载数据
    * @Param:
    * @create: 2020/3/6 15:00
    * @return:
    */
    @ResponseBody
    @RequestMapping(value = "findAll")
    public Layui findAll(@RequestParam(value = "e_id") Integer e_id,
                         @RequestParam(value = "page", required = false) Integer page,
                         @RequestParam(value = "limit", required = false) Integer limit,
                         @RequestParam(value = "e_name",required = false) String e_name,
                         @RequestParam(value = "e_number",required = false) String e_number) {
        DetachedCriteria criteria = DetachedCriteria.forClass(Equipment.class);
        criteria.add(Restrictions.eq("e_id", e_id));
        if(StringUtils.isNotEmpty(e_name)&&e_number!=null) {
            criteria.add(Restrictions.and(Restrictions.like("e_name", e_name, MatchMode.ANYWHERE),
                    Restrictions.like("e_number", e_number, MatchMode.ANYWHERE)));
        }else if (StringUtils.isNotEmpty(e_name)){
            criteria.add(Restrictions.like("e_name", e_name, MatchMode.ANYWHERE));
        }else if (StringUtils.isNotEmpty(e_number)){
            criteria.add(Restrictions.like("e_number", e_number, MatchMode.ANYWHERE));
        }
        List<Equipment> byConditions = equipmentService.findByConditions(criteria,page,limit);
        return Layui.data(0, "查询成功", equipmentService.getTotalCount(), byConditions);

    }

    /*
    * @Author: 周权
    * @Description: 设备管理列表页面的添加/修改数据
    * @Param:
    * @create: 2020/3/6 15:07
    * @return:
    */
    @ResponseBody
    @RequestMapping(value = "update")
    public Layui update(@RequestParam(value = "data", required = false) String data){
        JSONObject param = JSONObject.fromObject(data);
        if (param == null) {
            return Layui.data(1, "请求格式有误", 0, null);
        } else {
            String id1 = String.valueOf(param.get("id"));
            if(null==id1||id1.equals("")){
                Equipment equipment = (Equipment) JSONObject.toBean(JSONObject.fromObject(data), Equipment.class);
                equipment.setE_createDate(new Date());
                equipmentService.save(equipment);
                return Layui.data(0, "添加成功", 1, Layui.addList(equipment));
            }else{
                Equipment equipment = (Equipment) JSONObject.toBean(JSONObject.fromObject(data), Equipment.class);
                equipment.setE_createDate(new Date());
                equipmentService.saveOrUpdate(equipment);
                return Layui.data(0, "修改成功", 1, Layui.addList(equipment));
            }
        }
    }

    /*
    * @Author: 周权
    * @Description:设备管理删除功能
    * @Param:
    * @create: 2020/3/8 10:20
    * @return:
    */
    @ResponseBody
    @RequestMapping(value = "delete")
    public Layui delete(@RequestParam(value = "id") Integer id) {
        Equipment byId = equipmentService.findById(id);
        equipmentService.delete(byId);
        return Layui.data(1, "删除成功", 1, null);
    }

}
